//
//  OpenDownloadFileController.m
//  Etion
//
//  Created by JingXing Wu on 11-12-8.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "OpenFileViewController.h"
#import "ImageBrowserView.h"
#import "MediaPlayer/MediaPlayer.h"

static CFileView *s_fileView;

@interface CFileView() <CImageBrowserViewDelegate>
{
    UIViewController *_curRootViewController;
}

@property (nonatomic, assign) UIViewController *curRootViewController;
@property (nonatomic, copy) NSString *filePath;

@end

@implementation CFileView
{
    MPMoviePlayerViewController *moivePlayer;
}

@synthesize curRootViewController = _curRootViewController;

+ (CFileView *)getFileView
{
    if (s_fileView == nil)
    {
        s_fileView = [[CFileView alloc] init];
    }
    return s_fileView;
}

+ (void)releaseFileView
{
    [s_fileView release_mm];
    s_fileView = nil;
}

+ (EFileViewType)fileViewTypeOfFilePath:(NSString *)path
{
    NSString *fileext = [[[path lastPathComponent] pathExtension] lowercaseString];
    if ([fileext isEqualToString:@"png"] ||
        [fileext isEqualToString:@"jpg"])
    {
        return EFileViewTypeImage;
    }
    else if([fileext isEqualToString:@"doc"]||
            [fileext isEqualToString:@"docx"])
    {
        return EFileViewTypeWord;
    }
    else if ([fileext isEqualToString:@"ppt"]||
             [fileext isEqualToString:@"pptx"])
    {
        return EFileViewTypePPT;
    }
    else if ([fileext isEqualToString:@"pdf"])
    {
        return EFileViewTypePDF;
    }
    else if ([fileext isEqualToString:@"xls"]||
             [fileext isEqualToString:@"xlsx"])
    {
        return EFileViewTypeExcel;
    }
    else if ([fileext isEqualToString:@"txt"])
    {
        return EFileViewTypeTXT;
    }
    else if ([fileext isEqualToString:@"mp3"])
    {
        return EFileViewTypeAudio;
    }
    else if([fileext isEqualToString:@"mp4"] ||
            [fileext isEqualToString:@"3gp"] ||
            [fileext isEqualToString:@"m4v"] ||
            [fileext isEqualToString:@"mov"] ||
            [fileext isEqualToString:@"wav"] ||
            [fileext isEqualToString:@"aif"] ||
            [fileext isEqualToString:@"caf"])
    {
        return EFileViewTypeVideo;
    }
    
    return EFileViewTypeUnknown;
}

- (void)loadFailWithMessage:(NSString *)message
{
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release_mm];
}

+ (void)loadWithPath:(NSString *)path
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
    {
        [[CFileView getFileView] loadFailWithMessage:@"文件不存在！"];
        return;
    }
    
    [CFileView getFileView].filePath = path;
    EFileViewType type = [CFileView fileViewTypeOfFilePath:path];
    [CFileView getFileView].curRootViewController = [UIViewController currentRootViewController];
    switch (type)
    {
        case EFileViewTypeImage:
            [[CFileView getFileView] openImageFile:path];
            break;
        case EFileViewTypeWord:
        case EFileViewTypeExcel:
        case EFileViewTypePPT:
        case EFileViewTypePDF:
        case EFileViewTypeTXT:
            [[CFileView getFileView] openWebFile:path];
            break;
        case EFileViewTypeAudio:
        case EFileViewTypeVideo:
            [[CFileView getFileView] openMoviePlayerFile:path];
            break;
        default:
            [[CFileView getFileView] loadFailWithMessage:@"未知文件类型！"];
            break;
    }
}

#pragma mark open image

- (void)openImageFile:(NSString *)path
{
    CImageBrowserView *imageBrowserView = [[[CImageBrowserView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    imageBrowserView.delegate = self;
    [imageBrowserView showWithAnchorView:nil image:[UIImage imageWithContentsOfFile:path] index:0 fillStyle:EImageFillStyleNone complete:^{}];
}
    
- (NSUInteger)numberOfImagesForImageBrowserView:(CImageBrowserView *)imageBrowserView
{
    return 1;
}

- (id)imageBrowserView:(CImageBrowserView *)imageBrowserView cellInfoAtIndex:(NSUInteger)index
{
    CImageBrowserViewCellInfo *info = [[[CImageBrowserViewCellInfo alloc] init] autorelease];
    info.image = [UIImage imageWithContentsOfFile:self.filePath];
    return info;
}

- (void)imageBrowserViewNeedGetBack:(CImageBrowserView *)imageBrowserView
{
    [imageBrowserView dismissWithAnchorView:nil fillStyle:EImageFillStyleNone complete:^{}];
}

#pragma mark open web file

- (void)openWebFile:(NSString *)path
{
    /*
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    CViewBaseViewController *webFileViewController = [[[CViewBaseViewController alloc] initWithFrame:[UIScreen mainScreen].bounds]autorelease_mm];
    [webFileViewController createNavigationBar:@"文件查看" leftBtnType:XUIButtonStyleTypeCenterImage rightBtnType:XUIButtonStyleTypeNONE];
    [(XUIButton *)webFileViewController.lbtnItem.customview SetImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    UIButton *btn = (UIButton *)webFileViewController.lbtnItem.customview;
    [btn addTarget:self action:@selector(ViewBaseViewControllerLeftNavBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    UIWebView* webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, [CViewBaseViewController VclContentStartY]+44, webFileViewController.view.width, webFileViewController.view.height-[CViewBaseViewController VclContentStartY]-44)];
    webView.scalesPageToFit=YES;
    NSURL* url=[NSURL fileURLWithPath:path];
    NSURLRequest* request=[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    [webFileViewController.view addSubview:webView];
    [webView release_mm];
    
    [_curRootViewController presentViewController:webFileViewController animated:YES completion:^
    {
    }];
     */
}

#pragma mark open movie player

- (void)openMoviePlayerFile:(NSString *)path
{
    NSURL *url = [NSURL fileURLWithPath:path];
    moivePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [_curRootViewController presentMoviePlayerViewControllerAnimated:moivePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moivePlayFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)moivePlayFinish:(NSNotification *)noti
{
    [_curRootViewController dismissMoviePlayerViewControllerAnimated];
    [moivePlayer release_mm];
    moivePlayer = nil;
}

#pragma mark view base view controller delegate

- (void)ViewBaseViewControllerLeftNavBtnClicked:(id)sender
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    [_curRootViewController dismissViewControllerAnimated:YES completion:^
    {
    }];
}

@end
